#include "SingleImage.hpp"
#include "BaseFormat.hpp"
#include <filesystem>
#include <format>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>

void SingleImage::Start(const std::filesystem::path &savepath,
                        const BaseFormatAdditionalInf &additional) {
  if (std::filesystem::exists(savepath))
    throw std::runtime_error("can't open directory");
  if (!std::filesystem::create_directory(savepath))
    throw std::runtime_error("can't create directory");
  savePath = savepath;
  width = additional.imgWidth;
  height = additional.imgHeight;
  switch (additional.pixelType) {
  case BaseCamera::ImageType::GREY:
    channel = 1;
    break;
  case BaseCamera::ImageType::RGB8:
    channel = 3;
    break;
  }
}

void SingleImage::FileSaveFuncCall(const ImageType &image) {
  savePath.c_str();
  stbi_write_bmp((savePath / std::format("{}.bmp", index++)).c_str(), width,
                 height, channel, image.data);
}

void SingleImage::Stop() { index = 0; }

SingleImage::~SingleImage() {}
